package com.atguigu.lease.web.app.interceptor;

import com.atguigu.lease.common.advice.CostomReturnException;
import com.atguigu.lease.common.jwt.JwtUtil;
import com.atguigu.lease.common.result.ResultCodeEnum;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取token
        String accessToken = request.getHeader("access_token");
        //判断是否存在token，没有，返回未登录
        if (ObjectUtils.isEmpty(accessToken)){
            throw new CostomReturnException(ResultCodeEnum.APP_LOGIN_AUTH);
        }
        //验证token
        JwtUtil.parseToken(accessToken);
        //放行
        return true;
    }
}
